package ${basePackage}.cli.command;
import cn.hutool.core.bean.BeanUtil;
import ${basePackage}.generator.FileGenerator;
import ${basePackage}.model.DataModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import picocli.CommandLine.*;
import picocli.CommandLine;


import java.util.concurrent.Callable;


<#macro generateOption modelInfo >
    <#if modelInfo.fieldName?? >
        @Option(names = {<#if modelInfo.abbr??>"-${modelInfo.abbr}", </#if>"--${modelInfo.fieldName}"}, arity = "0..1", <#if modelInfo.description??>description = "${modelInfo.description}", </#if>interactive = true, echo = true)
        private ${modelInfo.type}  ${modelInfo.fieldName} = <#if modelInfo.defaultValue??> ${modelInfo.defaultValue?c};</#if>
    </#if>
</#macro>
<#-- 生成命令调用 -->
<#macro generateCommand  modelInfo>
    System.out.println("输入${modelInfo.groupName}配置：");
    CommandLine ${modelInfo.groupKey}CommandLine = new CommandLine(${modelInfo.type}Command.class);
    ${modelInfo.groupKey}CommandLine.execute(${modelInfo.allArgsStr});
</#macro>

@Command(name = "generate",mixinStandardHelpOptions = true,description = "生成代码")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class GenerateCommand implements Callable${'<'}Integer${'>'} {

    <#list modelConfig.models as modelInfo>

        <#--有分组-->
        <#if modelInfo.groupKey??>
            static DataModel.${modelInfo.type} ${modelInfo.groupKey} = new DataModel.${modelInfo.type}();
        <#-- 根据分组生成命令类-->
        @Command(name = "${modelInfo.groupKey}")
        @Data
        public static class ${modelInfo.type}Command implements Runnable {
            <#list modelInfo.models as subModelInfo>
                <@generateOption  modelInfo=subModelInfo />
            </#list>

            @Override
            public void run() {
            <#list modelInfo.models as subModelInfo>
                ${modelInfo.groupKey}.${subModelInfo.fieldName} = ${subModelInfo.fieldName};
            </#list>
            }
        }
        <#else>
            <@generateOption modelInfo=modelInfo/>
        </#if>


    </#list>

        <#-- 生成调用方法 -->
        public Integer call() throws Exception {
        <#list modelConfig.models as modelInfo>
            <#if modelInfo.groupKey??>
                <#if modelInfo.condition??>
                    if (${modelInfo.condition}) {
                    <@generateCommand  modelInfo=modelInfo />
                    }
                <#else>
                    <@generateCommand  modelInfo=modelInfo />
                </#if>
            </#if>
        </#list>
        <#-- 填充数据模型对象 -->
        DataModel dataModel = new DataModel();
        BeanUtil.copyProperties(this, dataModel);
        <#list modelConfig.models as modelInfo>
            <#if modelInfo.groupKey??>
                dataModel.${modelInfo.groupKey} = ${modelInfo.groupKey};
            </#if>
        </#list>
            FileGenerator.doGenerate(dataModel);
             return 0;
        }


}